package com.yanqu.road.server.cmd.beauty;

import com.yanqu.road.entity.config.beauty.BeautyFavorResult;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.module.player.ChildModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.pb.BeautyPb;
import com.yanqu.road.pb.beauty.BeautyPalyProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_BEAUTY_FAVOR, desc = "美女宠幸")
public class BeautyFavorCmd extends AbstractCommand {

    private List<Integer> beautyIdList;
    private int type;
    private int count; // 宠幸次数
    private boolean isQuick; // 一键宠幸

    private BeautyFavorResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BeautyPalyProto.BeautyFavorReqMsg req = BeautyPalyProto.BeautyFavorReqMsg.parseFrom(packet.getBytes());
        beautyIdList = req.getBeautyIdListList();
        type = req.getType();
        count = req.getCount();
        isQuick = req.getIsQuick();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (isQuick) {
            result = new BeautyFavorResult();
            if (count <= 0) {
                result.setRet(0);
                return;
            }

            if (count > 1) { // 当宠幸次数大于1时，比较子嗣空闲位置和消耗道具数
                count = Math.min(player.getModule(ChildModule.class).getRemainingChildSiteCount(), count);
            }
            result = player.getModule(BeautyModule.class).beautyQuickFavor(beautyIdList, type, count);
        } else {
            result = player.getModule(BeautyModule.class).beautyFavor(beautyIdList, type);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        BeautyPalyProto.BeautyFavorRespMsg.Builder respMsg = BeautyPb.parseBeautyFavorRespMsg(result);
        if(null != respMsg) {
            player.sendPacket(Protocol.U_BEAUTY_FAVOR, respMsg);
        }
    }
}
